using System;
using System.Windows.Forms;

using FridayShopping.DataModel;

namespace FridayShopping.UI
{
	public class ProducstListFormManager : ListFormManager 
	{
		private Key					currentCategoryKey;

		public event EventHandler	CategoryChanged;

		#region Constructor
		public ProducstListFormManager(IListViewForm form, ListView listView, Categories categories) : 
			base (form, listView)
		{
			list = Database.Instance.Categories.ProductsByCategory;
		}
		#endregion

		#region Member Access
		public Key CurrentCategoryKey
		{
			set
			{
				currentCategoryKey = value;
			}
			get
			{
				return currentCategoryKey;
			}
		}

		public int Count 
		{
			get
			{
				return list.List.Count;
			}
		}

		#endregion

		protected override ListViewItem CreateListViewItem(ItemBase item)
		{
			Product i = (Product)item;
			string price;
			if (i.Price > 0) 
			{
				price = i.Price.ToString();
			} 
			else 
			{
				price = "";
			}
			string[] subitems = {
									i.Key.ToString(),
									i.Name,
									price,
									i.Amount.ToString(),
			};
			return new ListViewItem(subitems);
		}

		public override void ToolbarEventsHandler(ToolBarButtonClickEventArgs e)
		{
			base.ToolbarEventsHandler (e);

			if (e.Button == ((ProductsForm)form).toolBarButton_Plus) 
			{
				Key key = GetItemKey(listView.FocusedItem);
				Product item = (Product)list.GetItem(key);
				if (item != null) 
				{
					item.Amount++;
					RefreshCurrentItem(item);
				}
			}
			if (e.Button == ((ProductsForm)form).toolBarButton_Minus) 
			{
				Key key = GetItemKey(listView.FocusedItem);
				Product item = (Product)list.GetItem(key);
				if (item != null) 
				{
					if (item.Amount >0) 
					{
						item.Amount--;
					}
					RefreshCurrentItem(item);
				}
			}
			if (e.Button == ((ProductsForm)form).toolBarButton_reusual)
			{
				if (MessageBox.Show(LangResources.Instance.Resources.GetString("resetToUsual"),
					LangResources.Instance.Resources.GetString("applName"),
					MessageBoxButtons.YesNo, MessageBoxIcon.Question,
					MessageBoxDefaultButton.Button2) == DialogResult.Yes)
				{
					RestoreUsualAmounts();
					FillListView();
				}
			}
		}

		private void RestoreUsualAmounts()
		{
			foreach(Product product in list.List)
			{
				product.SetAmountAsUsual();
			}
		}

		public override void EnableDisableItems()
		{
			bool toEnable = listView.SelectedIndices.Count > 0;
			((ProductsForm)form).toolBarButton_Minus.Enabled = toEnable;
			((ProductsForm)form).toolBarButton_Plus.Enabled = toEnable;
			base.EnableDisableItems ();
		}

		override protected void AddItem(ItemBase item)
		{
			base.AddItem(item);
			Product product = (Product)item;
			// Update the next key value
			Database.Instance.AddItem(product);
			// New item has different category and we now not in "All"
			if (!currentCategoryKey.Empty &&  
				currentCategoryKey != product.CategoryKey) 
			{
				// Update the current category
				currentCategoryKey = product.CategoryKey;
				// Raise event to signal that current category changed
				if (CategoryChanged != null) 
				{
					CategoryChanged(this, new EventArgs());
				}
			}
		}

		override protected bool ShouldAddToListView(ItemBase item)
		{
			if (currentCategoryKey.Empty) 
			{
				return true;
			}
			Product product = (Product)item;

			return (product.CategoryKey == currentCategoryKey);	
		}

		public override void SetDefaultValue(IItemPropertiesForm form)
		{
			form.SetDefaultValue(currentCategoryKey);
		}

		public override void HandleEditItemEvent()
		{
			Product oldProduct = (Product)GetItem(listView.FocusedItem);
			Key oldkey = new Key(-1);
			Key newkey = new Key(-1);
			if (oldProduct != null) 
			{
				oldkey = oldProduct.CategoryKey;
			}
			base.HandleEditItemEvent ();
			Product newProduct = (Product)GetItem(listView.FocusedItem);
			if (newProduct != null) 
			{
				newkey = newProduct.CategoryKey;
			}
			if (oldkey != newkey) 
			{
				CurrentCategoryKey = newkey;
				// Raise event to signal that current category changed
				if (CategoryChanged != null) 
				{
					CategoryChanged(this, new EventArgs());
				}
			}
		}

		// Update the master product list with the list displayed
		public void UpdateDatabase()
		{
			Database.Instance.AddItemsToCategories((Products)list);
			Database.Instance.Products.List = list.List;
		}
	}
}
